let db = require("../db/index");

// 添加文章
exports.add = (req, res) => {
    let sql1 = "insert into `essay` set ?";
    let data = req.body;
    data.content = data.content.replaceAll("\n", "</div><div>");
    data.content = "<div>" + data.content + "</div>";
    let date = Date.now();

    let obj = {
        id: "e" + date,
        title: data.title,
        content: data.content,
        author: data.author,
        authorid: data.authorId,
        t: date,
        views: 0,
        likes: 0,
        collect: 0,
        pic: JSON.stringify(data.pic),
        s: data.s,
        etype: data.etype,
        stype: data.stype,
        comment: "[]",
    };
    db.query(sql1, obj, (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "1上传失败" + err,
            });
        } else {
            let outside;
            if (typeof obj.authorid == "number") {
                outside = "backstage_user";
            } else {
                outside = "user";
            }
            let sql2 = `select essay from \`${outside}\` where \`id\` = ?`;
            db.query(sql2, obj.authorid, (err, data) => {
                if (err) {
                    if (err) {
                        return res.send({
                            s: 400,
                            m: "1上传失败" + err,
                        });
                    }
                }
                let arr;
                if (data[0].essay) {
                    arr = JSON.parse(data[0].essay);
                    arr.push(obj.id);
                } else {
                    arr = [];
                    arr.push(obj.id);
                }
                let str = JSON.stringify(arr);
                let sql3 = `update \`${outside}\` SET essay = ? where \`id\` = ?`;
                db.query(sql3, [str, obj.authorid], (err, data) => {
                    if (err) {
                        return res.send({
                            s: 400,
                            m: "上传失败" + err,
                        });
                    }
                    res.send({
                        s: 200,
                        m: "上传成功",
                    });
                });
            });
        }
    });
};

// 获取所有文章
exports.all = (req, res) => {
    let end = req.query.end;
    let type = req.query.type;
    let sql = `select * from \`essay\` ORDER BY \`${type}\` DESC`;
    db.query(sql, (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "获取失败",
            });
        }
        data.forEach((i) => {
            i.pic = JSON.parse(i.pic);
            i.comment = JSON.parse(i.comment);
            i.commentNum = i.comment.length;
            let sText;
            let stypeT;
            if (i.s == 0) {
                sText = "未审核";
            } else if (i.s == 1) {
                sText = "审核通过";
            } else if (i.s == 2) {
                sText = "审核不通过";
            }
            if (i.stype == 0) {
                stypeT = "全部";
            } else if (i.stype == 1) {
                stypeT = "热门";
            } else if (i.stype == 2) {
                stypeT = "生活";
            } else if (i.stype == 3) {
                stypeT = "学习";
            } else if (i.stype == 4) {
                stypeT = "体育";
            } else if (i.stype == 5) {
                stypeT = "游戏";
            } else if (i.stype == 6) {
                stypeT = "风景";
            } else if (i.stype == 7) {
                stypeT = "文案";
            }
            i.sText = sText;
            i.stypeT = stypeT;
        });
        if (end == "user") {
            data = data.filter((i) => {
                return i.s == 1;
            });
        }
        res.send({
            s: 200,
            m: "获取成功",
            data,
        });
    });
};

// 文章审核
exports.setEssay = (req, res) => {
    let id = req.body.id;
    let s = req.body.s;
    let sql = "UPDATE `essay` SET `s`= ? WHERE `id` = ?";
    db.query(sql, [s, id], (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "操作失败",
            });
        }
        res.send({
            s: 200,
            m: "操作成功",
        });
    });
};

// 通过分类获取文章
exports.classify = (req, res) => {
    let stype = req.query.stype;
    let sql = "select * from `essay` WHERE `stype` = ? ORDER BY `t` DESC";
    db.query(sql, stype, (err, data) => {
        if (err) {
            res.send({
                s: 400,
                m: "获取失败",
            });
        }
        data.forEach((i) => {
            i.pic = JSON.parse(i.pic);
            i.comment = JSON.parse(i.comment);
        });
        data = data.filter((i) => {
            return i.s == 1;
        });
        res.send({
            s: 200,
            data,
        });
    });
};

// 获取文章详细
exports.detailed = (req, res) => {
    let id = req.query.id;
    let sql = "select * from `essay` WHERE `id` = ?";
    db.query(sql, id, (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "获取失败",
            });
        }
        data.forEach((i) => {
            i.pic = JSON.parse(i.pic);
            i.comment = JSON.parse(i.comment);
        });
        res.send({
            s: 200,
            data,
        });
    });
};

// 观看量
exports.viewAdd = (req, res) => {
    let views = req.body.views;
    let id = req.body.id;
    let sql = "UPDATE `essay` SET `views`= ? WHERE `id` = ?";
    db.query(sql, [views, id], (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "添加失败",
            });
        }
        res.send({
            s: 200,
            m: "添加成功",
        });
    });
};
// 增加喜欢/收藏量
exports.setLC = (req, res) => {
    let num = req.body.num;
    let id = req.body.id;
    let type = req.body.type;
    let sql = `UPDATE \`essay\` SET \`${type}\`= ? WHERE \`id\` = ?`;
    db.query(sql, [num, id], (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "添加失败",
            });
        }
        res.send({
            s: 200,
            m: "添加成功",
        });
    });
};
// 模糊查询
exports.search = (req, res) => {
    let value = req.query.value;
    console.log(value);
    let sql = `select * from \`essay\` where \`title\` like \"%${value}%\" `;
    db.query(sql, (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "查询出错",
            });
        }
        console.log(data);
        data.forEach((i) => {
            i.pic = JSON.parse(i.pic);
        });
        res.send({
            s: 200,
            data,
        });
    });
};
